package cn.ljy.authority.web.resolver;


import cn.ljy.authority.annotation.LoginUserToken;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 与配合拦截器获用取用户的token信息，能将信息存储起来，供当次访问时在控制器注入当前token信息
 */
@Component
public class LoginUserTokenAugumentResolver implements HandlerMethodArgumentResolver {

    //判断Controller层中的参数，是否满足条件。如果满足条件则执行resolveArgument函数，不满足则跳过
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        //检查控制器的接口中是否存在@CurrentAdmin，如果存在则调用resolveArgument
        return parameter.hasParameterAnnotation(LoginUserToken.class);
    }

    //用于在控制器的参数中处理赋值
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        //获取当前登录用户的信息
        String token=(String)webRequest.getAttribute("token", RequestAttributes.SCOPE_REQUEST);
        return token;
    }
}
